package cn.gaozhong.ymcc.web.controller;


import cn.gaozhong.ymcc.exception.CustomException;
import cn.gaozhong.ymcc.result.AjaxResult;
import cn.gaozhong.ymcc.result.JSONResult;
import cn.gaozhong.ymcc.service.IVerifyCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifycode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;


    /**
     * 短信验证码
     * @param
     * @return
     */
    @GetMapping("/sendSmsCode/{mobile}")
    public JSONResult sendSmsCode(@PathVariable("mobile") String mobile){
            verifyCodeService.sendSmsCode(mobile);
            return JSONResult.success();

    }

    /**
     * 图形验证码
     * @param key
     * @return
     */
    @RequestMapping(value = "/imageCode/{imageCodeKey}",method = RequestMethod.GET)
    public AjaxResult imgCode(@PathVariable("imageCodeKey") String key) {
        try {
            String base64str = verifyCodeService.imgCode(key);
            return AjaxResult.me().setResultObj(base64str);
        } catch (CustomException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }

    }
}
